package com.cnlxu.database;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

/**
 * Created by cnlxu on 2016/8/8.
 */
public class DBHelper  extends SQLiteOpenHelper {
    public static final String DB_NAME = "words.db";
    public static final String DB_TABLE_NAME = "word";
    private static final int DB_VERSION=1;

    static public class TABLE_COLUMN{
        static String ID = "_ID";
        static String NAME = "NAME";
        static String TRANSLATION = "TRANSLATION";
        static String MORE = "MORE";
        static String COUNTER = "COUNTER";
        // 0 is  unfinised 1 is finished
        static String STATUS = "STATUS";
        static String WEEK = "WEEK";
        static String DATE = "DATE";
    }

    public static String TAG = "DBHelper";
    public DBHelper(Context context) {
        //Context context, String name, CursorFactory factory, int version
        //factory输入null,使用默认值
        super(context, DB_NAME, null, DB_VERSION);
    }
    //数据第一次创建的时候会调用onCreate
    @Override
    public void onCreate(SQLiteDatabase db) {

        //创建表
        String tableSql = "CREATE TABLE IF NOT EXISTS " +DB_TABLE_NAME+
                "("+TABLE_COLUMN.ID+" INTEGER PRIMARY KEY AUTOINCREMENT, "
                +TABLE_COLUMN.NAME+" VARCHAR, "
                +TABLE_COLUMN.TRANSLATION+" STRING, "
                +TABLE_COLUMN.MORE+" STRING, "
                +TABLE_COLUMN.WEEK+" INTEGER,"
                +TABLE_COLUMN.COUNTER+" INTEGER,"
                +TABLE_COLUMN.STATUS+" INTEGER,"
                +TABLE_COLUMN.DATE+" DATETIME DEFAULT CURRENT_TIMESTAMP)";
        db.execSQL(tableSql);
        Log.i(TAG, tableSql);
    }
    //数据库第一次创建时onCreate方法会被调用，我们可以执行创建表的语句，当系统发现版本变化之后，会调用onUpgrade方法，我们可以执行修改表结构等语句
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        //在表info中增加一列other
        //db.execSQL("ALTER TABLE info ADD COLUMN other STRING");
        Log.i("WIRELESSQA", "update sqlite "+oldVersion+"---->"+newVersion);
    }

}